
/* Copyright (c) 2024 Huawei Technologies Co., Ltd.
openFuyao is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN 'AS IS' BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */
import { Fragment, useState, useEffect, useStore } from 'openinula';
import { EditOutlined } from '@ant-design/icons';
import { Tag, message } from 'antd';
import { solveAnnotationOrLabelDiff } from '@/tools/utils';
import AnnotationModal from '@/components/AnnotationModal';
import { ResponseCode } from '@/common/constants';
import LabelTag from '@/components/LabelTag';
import { editAnnotationsOrLabels } from '@/api/schedulingApi';

export default function PriorityDetail({ details, refreshFn }) {
  const [messageApi, contextHolder] = message.useMessage();
  const [isAnnotationModalOpen, setIsAnnotationModalOpen] = useState(false);
  const [isLabelModalOpen, setIsLabelModalOpen] = useState(false);
  const [oldAnnotations, setOldAnnotataions] = useState(details?.metadata?.annotations ? details?.metadata?.annotations : '');
  const [oldLabels, setOldLabels] = useState(details?.metadata?.labels ? details?.metadata?.labels : '');

  // 注解成功回调
  const handleAnnotationOk = async (data) => {
    if (JSON.stringify(oldAnnotations) === JSON.stringify(data)) {
      messageApi.info('注解未进行修改');
      setIsAnnotationModalOpen(false);
    } else {
      const keyArr = [];
      data.map(item => keyArr.push(item.key));
      if (keyArr.filter((item, index) => keyArr.indexOf(item) !== index).length) {
        messageApi.error('存在相同key!');
      } else {
        // 请求接口添加
        // 比较前后是否一致 返回处理后的注解
        const addAnnotationList = solveAnnotationOrLabelDiff(oldAnnotations, data, 'annotation');
        try {
          const res = await editAnnotationsOrLabels(details.metadata?.name, addAnnotationList);
          if (res.status === ResponseCode.OK) {
            messageApi.success('编辑注解成功');
            setTimeout(() => {
              refreshFn();
              setIsAnnotationModalOpen(false);
            }, 1000);
          }
        } catch (error) {
          if (error.response.status === 403) {
            message.error('操作失败，当前用户没有操作权限，请联系管理员添加权限!');
          } else {
            message.error(`编辑注解失败！${error.response.data.message}`);
          }
        }
      }
    }
  };
  // 注解失败回调
  const handleAnnotationCancel = () => {
    setIsAnnotationModalOpen(false);
  };

  // 标签成功回调
  const handleLabelOk = async (data) => {
    if (JSON.stringify(oldLabels) === JSON.stringify(data)) {
      messageApi.info('标签未进行修改');
      setIsLabelModalOpen(false);
    } else {
      const keyArr = [];
      data.map(item => keyArr.push(item.key));
      if (keyArr.filter((item, index) => keyArr.indexOf(item) !== index).length) {
        messageApi.error('存在相同key!');
      } else {
        // 请求接口添加
        // 比较前后是否一致 返回处理后的注解
        const addLabelList = solveAnnotationOrLabelDiff(oldLabels, data, 'label');
        try {
          const res = await editAnnotationsOrLabels(details.metadata?.name, addLabelList);
          if (res.status === ResponseCode.OK) {
            messageApi.success('编辑标签成功');
            setTimeout(() => {
              refreshFn();
              setIsLabelModalOpen(false);
            }, 1000);
          }
        } catch (error) {
          if (error.response.status === 403) {
            message.error('操作失败，当前用户没有操作权限，请联系管理员添加权限!');
          } else {
            message.error(`编辑标签失败！${error.response.data.message}`);
          }
        }
      }
    }
  };

  // 标签失败回调
  const handleLabelCancel = () => {
    setIsLabelModalOpen(false);
  };

  return <Fragment>
    {contextHolder}
    <div className={`tab_container container_margin_box normal_container_height`} style={{ margin: '20px 32px' }}>
      <div className='detail_card'>
        <h3>基本信息</h3>
        <div className='detail_info_box'>
          <div className='base_info_list'>
            <div className='flex_item_opt'>
              <div className='base_description'>
                <p className='base_key'>priorityClass名称：</p>
                <p className='base_value'>{details.metadata?.name}</p>
              </div>
              <div className='base_description'>
                <p className='base_key'>优先级：</p>
                <p className='base_value'>{details.value}</p>
              </div>
              <div className='base_description'>
                <p className='base_key'>是否为默认优先级：</p>
                <p className='base_value'>{details.globalDefault ? '是' : '否'}</p>
              </div>
              <div className='base_description'>
                <p className='base_key'>是否为可被抢占：</p>
                <p className='base_value'>{details.preemptionPolicy === 'Never' ? '不可抢占低优先级的Pod' : '可抢占低优先级的Pod'}</p>
              </div>
            </div>
          </div>
          <div className='annotation'>
            <div className='ann_title'>
              <p>标签：</p>
              <EditOutlined className='primary_icon common_antd_icon' onClick={() => {
                setIsLabelModalOpen(true);
              }} />
            </div>
           <AnnotationModal open={isLabelModalOpen} type="label" dataList={details.metadata?.labels} callbackOk={handleLabelOk} callbackCancel={handleLabelCancel} />
            <div className="key_value">
              {details.metadata?.labels?.length ?
                details.metadata?.labels.map(item => <LabelTag labelKey={item.key} labelValue={item.value} theme='light' />) :
                <span style={{ marginTop: '13px' }}>0个</span>}
            </div>
          </div>
          <div className='annotation'>
            <div className='ann_title'>
              <p>注解：</p>
              <EditOutlined className='primary_icon common_antd_icon' onClick={() => {
                setIsAnnotationModalOpen(true);
              }} />
            </div>
           <AnnotationModal open={isAnnotationModalOpen} type="annotation" dataList={details.metadata?.annotations} callbackOk={handleAnnotationOk} callbackCancel={handleAnnotationCancel} />
            <div className='key_value'>
              {details.metadata?.annotations?.length ?
                details.metadata?.annotations.map(item =>
                  <LabelTag labelKey={item.key} labelValue={item.value} theme='light' />) :
                <span style={{ marginTop: '13px' }}>0个</span>}
            </div>
          </div>
        </div>
      </div>
    </div>
  </Fragment>;
}